package jch.learn.java.advanced.chapter4_network.socket;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class SocketTest {

    public static void main(String[] args) throws IOException {
        // java.net包下的网络编程接口与操作文件时使用的接口基本相同。
        try (Socket s = new Socket("www.baidu.com", 80); // 打开套件字，链接远程服务地址，若链接失败，将抛出UnknownHostException(父类是IOException)异常。
             Scanner in = new Scanner(s.getInputStream(), "UTF-8")) {
            ObjectOutputStream oops = new ObjectOutputStream(s.getOutputStream());
            oops.writeBytes("张三");
            oops.close();
            while (in.hasNextLine()) {
                String line = in.nextLine();
                System.out.println(line);
            }
        }
    }

}
